# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
from com.zzh.utils.requestFieldMapDBField import fieldMap_Zhao
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
import time
localTime = time.strftime('%Y-%m-%d', time.localtime(time.time()))

class CouponUsedDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertCouponUsed(self, info):
        """
        添加 coupon_used 信息
        :param info:
        :return:
        """
        couponId = info['couponId']
        userId = info['userId']
        timesUsed = info['timesUsed']

        sql = "insert into zzh_coupon_used (coupon_id, user_id, times_used) VALUES " \
              "(%s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [couponId, userId, timesUsed])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findCouponUsedById(self, couponId):
        """
        根据ID查询 coupon_used 信息
        :param couponId:
        :return:
        """
        sql = "select * from zzh_coupon_used WHERE id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [couponId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.coupon_used_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findCouponUsedByCondition(self, **kwargs):
        """
        coupon_used 表的条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        fieldMap = fieldMap_Zhao.couponUsedFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()

        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_coupon_used WHERE coupon_id in (SELECT coupon_id FROM zzh_coupon WHERE coupon_expirationdate > '"+localTime+"' ) AND "  + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_coupon_used WHERE coupon_id in (SELECT coupon_id FROM zzh_coupon WHERE coupon_expirationdate > '"+localTime+"' ) AND "  + whereSentence
        rs = cur.execute(sql)
        jsons = None
        if rs:
            rsSet = cur.fetchall()
            list0 = MyPaging().getPage(rs, pageIndex, pageSize, rsSet)
            jsons = Table2Json.coupon_used_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons, rs



    def deleteCouponUsed(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_coupon_used WHERE id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateCouponUsed(self, info):
        """
        更新 coupon_used 表
        :param info:
        :return:
        """
        mId = info['id']
        couponId = info['couponId']
        userId = info['userId']
        timesUsed = info['timesUsed']

        sql = "update zzh_coupon_used set coupon_id=%s,user_id=%s, times_used=%s WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [couponId, userId, timesUsed, mId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateCouponUsedByCouponIdAndUserid (self, userId, couponId):
        """
        更新 coupon_used 表
        :param info:
        :return:
        """
        couponId = couponId
        userId = userId
        timesUsed = 1
        status = 1

        sql = "update zzh_coupon_used set status=%s, times_used=%s WHERE user_id=%s and coupond_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [status, timesUsed, userId, couponId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs
    def selectCPused(self,uId):
        """
        根据ID查询，返回元组
        :param uId:
        :return: data tuple
        """
        sql = "select * from zzh_coupon_used WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [uId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr
